import win32gui, win32con, pyautogui, json

# mouse_position = ...



def task():
    global mouse_position
    windowname = mouse_position['windowname']
    screensize = mouse_position['screensize']
    globalposition = mouse_position['globalposition']
    windowposition = mouse_position['windowposition']
    windowsize = mouse_position['windowsize']

    hwnd = win32gui.FindWindow(None, windowname)
    if not hwnd:
        raise Exception(f"未找到标题为'{windowname}'的窗口。")

    foreground_hwnd = win32gui.GetForegroundWindow()  # 获取前台窗口句柄
    if foreground_hwnd != hwnd:
        win32gui.SendMessage(hwnd, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0)
        pyautogui.press("alt")
        win32gui.SetForegroundWindow(hwnd)

    # 计算当时的局部坐标
    local_x = (globalposition['x'] - windowposition['x']) / windowsize['width']
    local_y = (globalposition['y'] - windowposition['y']) / windowsize['height']
    # 计算新的全局坐标
    wl, wt, wr, wb = win32gui.GetWindowRect(hwnd)
    ww = wr - wl
    wh = wb - wt
    global_x = int(wl + ww * local_x)
    global_y = int(wt + wh * local_y)

    _x = global_x
    _y = global_y
        
    pyautogui.moveTo(_x, _y)
    
    return json.dumps({ 'success': True }, ensure_ascii=False)